package com.hsogoo.collate.quartz;

import com.hsogoo.collate.domain.CollateConfig;
import com.hsogoo.collate.service.CollateConfigManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by za-huangsaigang on 2018/11/20.
 */
@Slf4j
@Service
public class SchedulerBootstrap implements InitializingBean {

    @Autowired
    private CollateConfigManager collateConfigManager;
    @Autowired
    private SchedulerManager schedulerManager;
    @Autowired
    private SchedulerJobCreator schedulerJobCreator;

    @Override
    public void afterPropertiesSet() throws Exception {

        log.info("加载所有已配置的对账任务。。。");
        List<CollateConfig> taskList = collateConfigManager.getCollateConfigs();
        log.info("总共加载到{}个对账任务。。。", taskList.size());

        for(CollateConfig collateConfig : taskList){
            //TODO:暂时不考虑job状态
            log.info("添加任务...id:{},taskName:{},cron:{}",collateConfig.getId(),collateConfig.getTaskName(),collateConfig.getCron());
            SchedulerJob job = schedulerJobCreator.create(collateConfig);
            schedulerManager.addJob(job);
        }

        //添加一个定时扫描job
        SchedulerJob scanJob = schedulerJobCreator.createScanJob();
        schedulerManager.addJob(scanJob);
        log.info("添加任务扫描job任务成功");

    }
}
